package com.miyake.dsp.otdr.ui;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import com.miyake.dsp.otdr.Fiber;
import com.miyake.dsp.otdr.FiberManager;
import com.miyake.dsp.otdr.FiberProperties;
import com.miyake.dsp.otdr.NodeProperties;
import com.miyake.dsp.ui.DspComboBox;
import com.miyake.dsp.ui.DspTextField;
import com.miyake.dsp.ui.DspWidget;
import com.miyake.dsp.ui.DspWidget.DspWidgetHandler;


public class FiberPropertiesPanel extends AbstractOtdrPanel {
	private FiberManager manager;
	private DspTextField attenuation;
	private DspTextField bsc;
	private DspTextField ior;
	private NodeProperties nodeProp;
	private DspComboBox combo;
	private DspTextField reflectance;
	private DspTextField loss;
	private DspTextField length;
	
	public FiberPropertiesPanel(NodeProperties nodeProp, FiberManager manager) {
		super();
	
		this.manager = manager;
		this.nodeProp = nodeProp;
		initialize();
	}

	private void initialize() {
		setSize(500, 200);
		Container container = this;
		container.setLayout(new FlowLayout());
		
		JPanel group = new JPanel();
		container.add(group);
		group.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		group.setLayout(new GridLayout(4,1));
		group.add(createName());
		group.add(createIOR());
		group.add(createBSC());
		group.add(createAttenuation());

		JPanel group2 = new JPanel();
		container.add(group2);
		group2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		group2.setLayout(new GridLayout(3,1));
		group2.add(createLength());
		group2.add(createLoss());
		group2.add(createReflectance());
		
		updateValues(nodeProp.getFiber().getName());
	}

	@Override
	protected void apply() {
		nodeProp.setLength(length.getValue());
		nodeProp.setLoss(loss.getValue());
		nodeProp.setReflectance(reflectance.getValue());
		setVisible(false);
	}

	private Component createLength() {
		length = new DspTextField("Length (km)");
		length.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				nodeProp.setLength(Double.valueOf(value));
				fireChange("");
			}
		});
		return length;
	}

	private Component createReflectance() {
		reflectance = new DspTextField("Reflectance (dB)");
		reflectance.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				nodeProp.setReflectance(Double.valueOf(value));
				fireChange("");
			}
		});
		return reflectance;
	}

	private Component createLoss() {
		loss = new DspTextField("Loss (dB)");
		loss.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				nodeProp.setLoss(Double.valueOf(value));
				fireChange("");
			}
		});
		return loss;
	}

	private DspTextField createAttenuation() {
		attenuation = new DspTextField("Attenuation (dB/km)");
		attenuation.setEditable(false);
		return attenuation;
	}

	private DspTextField createBSC() {
		bsc = new DspTextField("BSC (dB)");
		bsc.setEditable(false);
		return bsc;	
	}

	private DspTextField createIOR() {
		ior = new DspTextField("IOR @" + String.valueOf(nodeProp.getWavelength()) );
		ior.setEditable(false);
		return ior;
	}

	private DspComboBox createName() {
		List<String> list = new ArrayList<String>();
		for (String s : manager.getNameList()) {
			list.add(s);
		}
		combo = new DspComboBox("Fiber", list);
		combo.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				updateValues(value);
				fireChange("");
			}
		});
		return combo;
	}

	protected void updateValues(String name) {
		Fiber fiber = manager.getFiber(name);
		FiberProperties fiberProp = fiber.getProperties(nodeProp.getWavelength());
		DecimalFormat exFormat1 = new DecimalFormat("0.000"); 
		attenuation.setValue(exFormat1.format(fiberProp.getAttenuation()));
		bsc.setValue(String.valueOf(fiberProp.getBsc()));
		ior.setValue(String.valueOf(fiberProp.getIor()));
		
		length.setValue(String.valueOf(nodeProp.getLength()));
		loss.setValue(String.valueOf(nodeProp.getLoss()));
		reflectance.setValue(String.valueOf(nodeProp.getReflectance()));
		
		nodeProp.setFiberProp(fiberProp);
		combo.setSelected(name);
	}
}
